package com.ship.boats.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.ship.boats.business.UploadPortInformationBusiness;
import com.ship.boats.domain.SpBoatsPortMessage;
import com.ship.boats.domain.SpPortInformation;
import com.ship.boats.domain.dto.QueryPortMessageRq;
import com.ship.boats.domain.dto.QueryTimeZoneSwitchingRq;
import com.ship.boats.domain.dto.UpdatePortMessageRq;
import com.ship.boats.domain.vo.QueryPortMessageInfo;
import com.ship.boats.domain.vo.QueryPortNameInfo;
import com.ship.boats.domain.vo.QueryTimeZoneSwitchingInfo;
import com.ship.boats.service.BoatsPortMessageService;
import com.ship.boats.service.PortInformationService;
import com.ship.boats.util.ExportDownloadUtil;
import com.ship.common.core.exception.ServiceException;
import com.ship.common.core.utils.FileUtils;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.common.core.web.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>PortInformationController</b>
 *
 * @description: PortInformationController <br>
 * @date: 2024/4/29 11:49 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "港口信息")
@RestController
public class PortInformationController {
    private static final Logger logger = LoggerFactory.getLogger(PortInformationController.class);

    @Autowired
    private BoatsPortMessageService boatsPortMessageService;

    @Autowired
    private UploadPortInformationBusiness uploadPortInformationBusiness;

    @Autowired
    private ExportDownloadUtil exportDownloadUtil;

    @ApiOperation("上传港口信息")
    @PostMapping("/upload/portInformation")
    public JsonResult uploadPortInformation(@RequestParam("file") MultipartFile file){
        FileUtils.checkFileName(file);
        List<SpBoatsPortMessage> portList = uploadPortInformationBusiness.uploadList(file);
        boolean status = boatsPortMessageService.createPortInformation(portList);
        return JsonResult.success(status);
    }

    @ApiOperation("查询港口信息查询条件信息")
    @GetMapping("/query/portMessage/conditions")
    public JsonResult queryPortMessageConditions(@RequestParam String type){
        List<String> returnList = boatsPortMessageService.queryPortMessageConditions(type);
        return JsonResult.success(returnList);
    }


    @ApiOperation("分页查询港口信息列表")
    @PostMapping("/query/portMessage/list")
    public JsonResult queryPortMessageList(@RequestBody QueryPortMessageRq rq){
        logger.info("分页查询港口信息入参:{}"+ JSONUtil.parse(rq));
        PageInfo<QueryPortMessageInfo> pageInfo = boatsPortMessageService.queryPortMessageList(rq);
        logger.info("分页查询港口信息列表返回结果:{}"+JSONUtil.parse(pageInfo));
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("根据港口ID查询港口详情")
    @GetMapping("/query/portMessage{id}")
    public JsonResult queryPortMessageById(@PathVariable Long id){
        SpBoatsPortMessage info = boatsPortMessageService.queryPortMessageById(id);
        return JsonResult.success(info);
    }

    @ApiOperation("导出港口信息列表")
    @PostMapping("/export/portMessage")
    public JsonResult exportPortMessage(@RequestBody QueryPortMessageRq rq){
        logger.info("导出港口信息列表入参:{}"+JSONUtil.parse(rq));
        List<QueryPortMessageInfo> portMessageList = boatsPortMessageService.exportPortMessage(rq);
        Field[] fields = QueryPortMessageInfo.class.getDeclaredFields();
        List<String> tableList = new ArrayList<>();
        tableList.add("portNameEn");
        tableList.add("countryEn");
        tableList.add("continentName");
        tableList.add("timeZone");
        tableList.add("lon");
        tableList.add("lat");
        tableList.add("type");
        tableList.add("maximumDraft");
        tableList.add("maxDwt");
        tableList.add("maxLoa");
        tableList.add("maxBeam");
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("港口名称");
        tableNameList.add("港口国别");
        tableNameList.add("区域");
        tableNameList.add("时区");
        tableNameList.add("经度");
        tableNameList.add("纬度");
        tableNameList.add("类型");
        tableNameList.add("最大吃水");
        tableNameList.add("最大DWT");
        tableNameList.add("最大船长");
        tableNameList.add("最大宽度");
        String url = exportDownloadUtil.exportUtil(portMessageList,fields,
                tableList,tableNameList,
                "港口信息");
        return JsonResult.success(url);
    }

    @ApiOperation("根据港口ID修改港口备注信息")
    @PostMapping("/update/portMessageById")
    public JsonResult updatePortMessageById(@RequestBody UpdatePortMessageRq rq){
        boolean status = boatsPortMessageService.updatePortMessageById(rq);
        return JsonResult.success(status);
    }

    @ApiOperation("查询所有港口名称")
    @GetMapping("/query/portName/all")
    public JsonResult queryPortNameAll(@RequestParam(required = false) String portName){
        List<QueryPortNameInfo> portNameList = boatsPortMessageService.queryPortNameAll(portName);
        return JsonResult.success(portNameList);
    }

    @ApiOperation("时区转换")
    @PostMapping("/timeZone/switching")
    public JsonResult timeZoneSwitching(@RequestBody QueryTimeZoneSwitchingRq rq) throws ParseException {
        QueryTimeZoneSwitchingInfo info = boatsPortMessageService.timeZoneSwitching(rq);
        return JsonResult.success(info);
    }
}
